var currentTreeNode = null;
var treeObj;
var minForm;

function treeselected(e) {
    var tree = e.sender;
    var node = e.node;
    currentTreeNode = node;
    $("#edit_node").css("display", "block");
    $("#sBtn").css("display", "block");

    if (node) {
        var name = node.name;
        var codeName;
        var splitName = name.split("_");
        var entityName = "";
        var entity = "";
        var entityId = "";
        for (var i = 0; i < splitName.length; i++) {
            entity += splitName[i].substr(0, 1).toUpperCase() + splitName[i].substr(1).toLowerCase();
            if (splitName[i] == "TB" || splitName[i] == "tb") {
                continue;
            }
            entityName += splitName[i].substr(0, 1).toUpperCase() + splitName[i].substr(1).toLowerCase();
        }
        if (name.substr(0, 2) == "TB" || name.substr(0, 2) == "tb") {
            codeName = name.substr(3, 1).toUpperCase() + name.substr(4).toLowerCase();
            //alert(codeName);
            entityId = name.substr(3).toLowerCase() + "Id";

        } else {
            if (name.indexOf("_") != -1) {
                codeName = name.substr(0, 1).toUpperCase() + name.substr(1).replace(new RegExp('_', 'g'), "").toLowerCase();
                entityId = name.substr(0, 1) + name.substr(name.indexOf("_") + 1, 1).toLowerCase() + "Id";
            } else {
                codeName = name.substr(0, 1).toUpperCase() + name.substr(1).toLowerCase();
                entityId = name + "Id";
            }
        }
        mini.get("serviceName").setValue(entityName);
        mini.get("daoName").setValue(entityName + "Dao");
        mini.get("entityName").setValue(entity);
        mini.get("actionName").setValue(entityName);
        mini.get("entityId").setValue(entityId);
        mini.get("table").setValue(name);
        var sourceName=$("#sources").val();
        ActHttpRequest('generate!tableColumn.do', {'generate.tableName': name,'chectkDataSource':sourceName}, function (data) {
            if (data.OK == "OK") {
                document.getElementById("listName").innerHTML = data.message;
                mini.get("entityId").setValue(data.entityId);
            } else {
                mini.alert(data.message);
            }
        }, "post", false, null, null, true);
    }
}

/***
 * 加载树
 * */
function initTree() {
    ActHttpRequest('generate/tableName.do', {}, function (data) {
        if (data.OK == "OK") {
            var nodeArry = [];
            for (var i = 0; i < data.tableName.length; i++) {
                nodeArry.push({id: i, name: data.tableName[i], pid: ''});
            }
            treeObj.loadList(nodeArry, 'id', 'pid');
        }
    }, "post", false, null, null, true);
}

/***
 * 编辑选择的树节点
 * */
function editOrgData() {
    var minForm = new mini.Form("#form1")
    minForm.validate();
    if (minForm.isValid() == false) {
        mini.alert("验证失败，点击页面红色叹号查看！");
        return;
    }
    var formData = serializeQuery("form1");
    formData["chectkDataSource"]=$("#sources").val();
    ActHttpRequest("generate!generate.do", formData, function (data) {
        if (data.OK == "OK") {
            mini.alert(data.message);
        } else {
            mini.alert(data.message);
        }
    }, "post", false, null, '保存中。。。');
}

$(document).ready(function () {
    treeObj = mini.get("tree");
    minForm = new mini.Form("#form1");
    initTree();
    //获取数源
    getsources();
});

function getsources(){
    ActHttpRequest('generate/getSelectDataSource.do', {}, function (data) {
        if (data.OK == "OK") {
            var html="";
            for (var i=0;i<data.sources.length;i++){
                html+="<option value=\""+data.sources[i]+"\">"+data.sources[i]+"</option>"
            }
            $("#sources").append(html);
        }
    }, "post", false, null, null, true);
}

function sourceselect(){
    var sourceName=$("#sources").val();
    ActHttpRequest('generate/tableName.do', {"chectkDataSource":sourceName}, function (data) {
        if (data.OK == "OK") {
            var nodeArry = [];
            for (var i = 0; i < data.tableName.length; i++) {
                nodeArry.push({id: i, name: data.tableName[i], pid: ''});
            }
            treeObj.loadList(nodeArry, 'id', 'pid');
        }
    }, "post", false, null, null, true);
}

function clearData() {
    minForm.reset();
}

function allCheck(t, name) {
    if (t.checked) {
        $("input[name='" + name + "']").each(function () {
            this.checked = true;
        });
    } else {
        $("input[name='" + name + "']").each(function () {
            this.checked = false;
        });
    }
}

function serializeQuery(form) {
    var serializeObj = {};
    var array = $('#' + form).serializeArray();
    $(array).each(function () {
        if (serializeObj[this.name]) {
            serializeObj[this.name] = serializeObj[this.name] + "-" + this.value;
        } else {
            serializeObj[this.name] = this.value;
        }
    });
    return serializeObj;
}